
library(raster)
library(sqldf)

inputPath= "C:/Users/forever/Desktop/SongHong"
fileArray = list.files(path = inputPath,pattern=".tif$",full.names = TRUE,recursive=TRUE)
totalFile = length(fileArray)
stack_raster=stack()

for(i in 1:totalFile){
	stack_raster=addLayer(stack_raster,fileArray[i])
}
daily_raster=mean(stack_raster,na.rm=TRUE)


out_file=paste("C:/Users/forever/Desktop/mydTB/2014-3.tif",sep="")

writeRaster(daily_raster,filename=out_file,format="GTiff")


library(raster)

inputPath= "C:/Users/forever/Desktop/TayBac"
listData = read.csv(paste(inputPath,"/list.csv",sep=""))

## data_avg = sqldf("select month,count(filename) as images from data group by area_id,Month;")
## write.csv(data_avg,"C:/AvgTempPress2.csv")

for(i in 1:12){
	data = subset(listData,listData$month==i)
	totalFile = nrow(data)
	if(totalFile>0){
		stack_raster=stack()
		for(j in 1:totalFile){
			filename = paste(inputPath,"/",data$filename[j],sep="")
			stack_raster=addLayer(stack_raster,filename)
		}
		daily_raster=mean(stack_raster,na.rm=TRUE)
		out_file=paste(inputPath,"/","Thang",i,".tif",sep="")
		writeRaster(daily_raster,filename=out_file,format="GTiff")
	}
	
}
print("done")





library(raster)
library(sqldf)

inputPath= "C:/Users/forever/Desktop"
data = read.csv(paste(inputPath,"/list_v2.csv",sep=""))

data_avg = sqldf("select month,count(filename) as images from data group by month;")
write.csv(data_avg,"C:/Users/forever/Desktop/vung.csv")
